<?php


/*
 *  A Part of snock project
 *  Most  operation of admin
 *   Author king-of-war
 * Last modified 2009-4-5
 */
class verify {

	/*
	 * $conn: instance of conn class
	 */
	private $conn;
	private $err;
	private $root_path;
	/*
	 * Initialization  variables and clss
	 */
	function __construct($root_path) {
		/*
		 * load connection class
		 */
		$this->set_root_path($root_path);
		include_once $root_path . 'includes/conn.php';
		$conn = new conn();
		$this->set_conn($conn);
		/*
		 * load error report class
		 */
		include_once $root_path . 'admin/class/error.class.php';
		$err = new Errmsg();
		$this->set_err($err);
		include_once $root_path . 'includes/xml/xml_op.php';
	}
	/*
	 * isMember: check the user's right with name and pass
	 * return the right
	 */
	function isMember($name, $pass) {
		$root_path = $this->get_root_path();
		$right = 0;
		if (isset ($name) && isset ($pass)) {
			$conn = $this->get_conn();
			$pass = sha1($pass);
			$sql = sprintf("SELECT users.id,users.name,E_mail, role,groups.rights FROM users
						RIGHT JOIN groups ON groups = groups.id where name='%s' and pass='%s' ", $name, $pass);
			$conn->set_sql($sql);

			$line = $conn->get_line();
			if ($line) {
				$result = $conn->get_result();
				$row = $result->fetch_assoc();
				$id = ($row['id']);
				$name = $row['name'];
				$right = $row['rights'];
				$role = $row['role'];
				$mail = $row['E_mail'];
			} else {
				$right = 0;
			}
			if ($right) {

				if ($right > 1) {
					$_SESSION['name'] = $name;
					/*
								 * set administrator infomation
								 */
					$root_path = $this->get_root_path();
					$xml = new xml_op($root_path);
					$xml->xml_admin_set($id, $name, $right, $role, $mail);

				} else {
					$err = $this->get_err();
					$info = '登陆失败';
					$err->error_chinese($info);
					exit;
				}
			} else {
				$right = 0;
			}
		}
		return $right;
	}
	/*
	 * functions for check identity
	 */

	function identity_check($right) {

		if (!empty ($_SESSION['name'])) {
			/*
					 * read the right
					 */

			$root_path = $this->get_root_path();
			$xml = new xml_op($root_path);
			$user = $xml->xml_admin_get();

			$iden = $user['right'];
			if ($right > $iden) {
				$err = $this->get_err();

				echo '<META HTTP-EQUIV="Refresh" CONTENT="2;URL=login.php">';
                     $info = '权限不足 ';
			//	$info = 'make sure you have the administrator identity  right ';
				$err->error_chinese($info);

			}

		} else {
			$err = $this->get_err();

			echo '<META HTTP-EQUIV="Refresh" CONTENT="2;URL=login.php">';

		  $info = 'Please login first......<br><br> && make sure you have the administrator identity  right ';
			$err->error_chinese($info);

			exit;
		}

	}
	/*
	 * functions for use above
	 */
	function set_conn($conn) {
		$this->conn = $conn;
	}
	function get_conn() {
		return $this->conn;
	}
	function set_err($err) {
		$this->err = $err;
	}
	function get_err() {
		return $this->err;
	}

	function set_root_path($root_path) {
		$this->root_path = $root_path;
	}
	function get_root_path() {
		return $this->root_path;
	}

	/*
	 * The end of this project
	 */
}
?>